package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements MemberService {


    @Autowired
    MemberDao dao;


    @Override
    public Member findByTelephone(String telephone) {

        return dao.findBytelephone(telephone);
    }

    @Override
    public void add(Member member) {
        dao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {

        List<String> months = new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        //获取前12个月
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }
        //统计每个月对应的会员数量
        for (String month : months) {
            month+="-31";
            long count = dao.findCountByDate(month);
            memberCount.add(count);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return map;
    }
}
